package croe.jdbc.dao.impl;

import croe.jdbc.common.AppContextSupport;
import croe.jdbc.dao.VoidWrapper;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;

/**
 * User: zengzhili
 * Time: 2014-07-03 21:48
 */
public class TransactionManager {
    private static DataSourceTransactionManager transactionManager;
    static {
        transactionManager = (DataSourceTransactionManager) AppContextSupport.getAppContext().getBean("transactionManager");
    }

    public static void commit(VoidWrapper voidWrapper){
        TransactionStatus status = null;
        try{
            status = transactionManager.getTransaction(null);
            voidWrapper.doWork();
            transactionManager.commit(status);
        }catch (Exception e){
            transactionManager.rollback(status);
            throw new RuntimeException(e);
        }
    }


}
